package com.example.haojiutao.controller.admin;

import com.example.haojiutao.common.Result;
import com.example.haojiutao.model.domain.Comment;
import com.example.haojiutao.model.domain.User;
import com.example.haojiutao.model.vo.response.CommentGoodsDetailVO;
import com.example.haojiutao.service.CommentService;
import com.example.haojiutao.service.UserService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @Description: 添加删除评论
 * @Author: zhou_yuan
 * @CreateTime： 2024/1/20 15:43
 */
@Slf4j
@RestController
public class CommentAdminController {
    @Autowired
    private CommentService commentService;

    @Autowired
    private UserService userService;
    /*
     * @Description: 评论商品
     * @Author: zhou_yuan
     * @CreateTime： 2024/1/20 15:45
     * @param: comment
     * @return: com.example.haojiutao.common.Result
     **/
    @PostMapping("/admin/comment")
    public Result instert(@RequestBody Comment comment){

        comment.setCommentTime(new Date());

        log.info("评论",comment);
        int rows= commentService.insert(comment).getCode();
        if(rows==1){
            return Result.faile("提交失败");
        }

        return Result.success("评论成功");
    }
    /*
     * @Description: 删除商品
     * @Author: zhou_yuan
     * @CreateTime： 2024/1/20 15:48
     * @param: id
     * @return: com.example.haojiutao.common.Result
     **/
//    @DeleteMapping("/comment")
//    public Result delete(int id){
//
//        return commentService.deleteById(id);
//    }
    @DeleteMapping("/admin/comment/{id}")
    public Result delete1(@PathVariable("id") int id){

        return commentService.deleteById(id);
    }
    /*
     * @Description: 查看所有评论
     * @Author: zhou_yuan
     * @CreateTime：2024/1/20 15:55
     * @param: comment
     * @return: com.example.haojiutao.common.Result
     **/
    @GetMapping("/admin/comment/list")
    public Result selectbylist(Comment comment){
        log.info("列表查询参数 ：{}" ,comment);
        List<Comment> comments =commentService.listByComment(comment);
        PageInfo<Comment> pageInfo=new PageInfo<>(comments);
        return Result.success("查询成功",comments);
    }


    /*
     * @Description: //根据用户ID查询,查询该博客下的所有评论
     * @Author: zhou_yuan
     * @CreateTime：2024/1/20 15:58
     * @param: userId
     * @return: com.example.haojiutao.common.Result
     **/
@GetMapping("/comment/admin/{userName}")
public Result selectByUserId(@PathVariable("userName") String userName){
    log.info("列表查询参数 ：{}");
    List<Comment> comments =commentService.selectByUserIdComments(userName);
    //PageInfo<Comment> pageInfo=new PageInfo<>(comments);
    for(Integer i = 0; i < comments.size(); i++) {
        Comment comment=new Comment();
        comment.setUserName(userName);
        comment.setContent(comment.getContent());
        comment.setGoodsId(comment.getGoodsId());
        comment.setLevel(comment.getLevel());
        comment.setCommentTime(new Date());
// 如果是回复其他用户，设置回复对象的id和用户名
        String touserName =comments.get(i).getToUserName();
        if(touserName != null){
            comment.setToCommentId(comment.getToCommentId());
            comment.setToUserName(userName);
            User reployToUser=userService.getUserInfoByName(userName);
            comment.setToUserName(reployToUser.getUserName());

        }

        User user=userService.getUserInfoByName(comments.get(i).getUserName());
        String UserName= comment.getUserName();
        if(UserName != null){
            comment.setUserName(user.getUserName());
            comments.add(comment);
        }

    }
    return Result.success("comments",comments);


}
    /*
     * @Description: 根据id查询
     * @Author: zhou_yuan
     * @CreateTime：2024/1/20 16:09
     * @param: commentId
     * @return: com.example.haojiutao.common.Result
     */
    @GetMapping("/admin/comment/{commentId}")
    public Result selectById(@PathVariable Integer commentId){
        CommentGoodsDetailVO comment = new CommentGoodsDetailVO();
        log.info("查看单个商品");
        comment = commentService.selectByCommentId(commentId);
        return Result.success("查询成功！",comment);
    }

}

